package tech.aistar.interceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 本类用来演示: 登录拦截
 *
 * @author: success
 * @date: 2021/5/10 1:35 下午
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {


    /**
     * 进入handler之前
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("preHandle...");

        //HttpSession session = request.getSession();
        //从session作用域中获取user对象
        //判断user是否为null
        //如果为null -
        // response.sendRedirect("");
        // return false;

        //return false - 拦截器,不放行
        //return true - 放行
        return true;
    }

    /**
     * 进入handler之后,但是返回modelAndView对象之前
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle...");
    }

    /**
     * 执行handler之后
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("after...");
    }
}
